/***************************************************************************
 *   Copyright (C) 2009 by umbacos@gmail.com                               *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/

#include "udm-common.h"

#define BUF_DIM 131072  /* 128 kB */
#define MAX_LEN 2097152 /*   2 mB */
#define S_FILENAME "filename=\""

char* dcgi;
char* droo;
char* dbin;
char* dwww;
char* dufb;
char* ddnl;
char* dtrk;
char* fcnf;
char* filter;
char* argv0;
char* pidfile;
char* wget;
char* ctor;
char* mtomb_start;
char* mtomb_stop;

void * xmalloc ( size_t size );
void get_directories ( char * file, int is_dump_cnf );
int filter_select ( struct dirent *entry );
void print_headers ( int final );
void send_message ( char * msg, char * inf );
char * pipe_cmd ( const char * cmd );
//char * c_to_utf_8 ( unsigned char c );

/* rfc2388 */
int rfc2388_handler () {
//	unsigned long maxread, content_length;
	char boundary[500];
	char fnam[500];
	char *cookie;
	char *curDir;
	char buf [BUF_DIM];
	char *str;
	char *iniz;
	long br;
	long pos;
	long len;
	char *fine;
	FILE * fd;
	int r;

#ifdef DEBUG
	mylog ( "COOKIES:", getenv("HTTP_COOKIE") );
#endif
	cookie = strdup( getenv("HTTP_COOKIE") );
	curDir = strstr( cookie, "curdir=" ) + 7;
	str = index ( curDir,';' );
	if (str != NULL) str[0]='\0';
	unescape_url ( curDir );
	curDir++;

#ifdef DEBUG
	mylog ( "curDir:", curDir );
#endif

	/* get the boundary info */
	str = strstr ( getenv ( "CONTENT_TYPE" ), "--" );

	if ( str ) {
		sprintf ( boundary, "\r\n--%s" , strstr ( getenv ( "CONTENT_TYPE" ), "--" ) );
		str = index ( boundary, '"' );

		if ( str ) str[0] = '\0';
	} else
		send_message ( "errore CONTENT_TYPE:%s\n", getenv ( "CONTENT_TYPE" ) );

	/* find filename */
	memset ( buf, 0, BUF_DIM );
	br = 1;
	while ((( iniz = strstr ( buf, S_FILENAME ) ) == NULL ) && (br != 0)) {
		buf[0]='\0';
		br = read ( STDIN, &buf, BUF_DIM );
#ifdef DEBUG
	sprintf ( fnam, "buf read %ld/%s:", br, getenv ( "CONTENT_LENGTH" ) );
	mylog ( fnam, buf );
#endif
	}

	if ( ( iniz = strstr ( buf, S_FILENAME ) ) == NULL )
		send_message ( "Upload failed: no filename received", "" );

	iniz += strlen ( S_FILENAME );

	if ( ( fine = index ( iniz, '"' ) ) == NULL )
		send_message ( "Upload failed: no filename termination", "" );

	strncpy ( fnam, iniz, fine - iniz );
	fnam[ fine - iniz ] = '\0';
	if ( rindex ( fnam, '\\' ) ) strcpy ( fnam, rindex ( fnam, '\\' ) + 1 );
	if ( rindex ( fnam, '/' ) ) strcpy ( fnam, rindex ( fnam, '/' ) + 1 );

#ifdef DEBUG
	mylog("UPLOAD FILENAME:",fnam);
#endif

	/* set pointer to where the file starts */
	if ( ( iniz = strstr ( fine, "\r\n\r\n" ) ) == NULL )
		send_message ( "Upload failed: no file", "" );

	iniz += 4;

	/* start to dump while searching for the final boundary */
	r = chdir ( ddnl );
	if ( strlen(curDir) > 0 ) r = chdir ( curDir );

#ifdef DEBUG
	mylog("UPLOAD DIRECTORY:",pipe_cmd("pwd"));
#endif
	if ( ( fd = fopen ( fnam, "w" ) ) == NULL )
		send_message ( "Upload failed: unable to save file", "" );

	do {
		pos = 0;
		len = br - strlen ( boundary ) + buf - iniz;

		while ( memcmp ( boundary, iniz + pos, strlen ( boundary ) ) && ( pos < len ) )
			fputc ( iniz [pos++], fd );

		if ( pos == len ) {

			memmove ( buf, iniz + pos, strlen ( boundary ) );
			iniz = buf;
			br = read ( STDIN, buf + strlen ( boundary ), BUF_DIM - strlen ( boundary ) ) + strlen ( boundary );
		}
	} while ( pos == len );

	fclose ( fd );

	return ( 0 );
}

int main ( int argc, char** argv ) {

	get_directories ( strdup ( argv[0] ), FALSE );
	rfc2388_handler();
	send_message ( "Location", "/" );

	return 0;
}
